package com.axend.aerosense.base.database.room;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Room;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.axend.aerosense.base.bean.RoomTypeBean;

import java.util.List;

public class RoomTypeMananger {

    private static RoomTypeDataBase mDb;
    public static RoomTypeDataBase getInstance(Context context) {
        if (mDb == null) {
            mDb = Room.databaseBuilder(context, RoomTypeDataBase.class, "room.db")
                    .allowMainThreadQueries()
                    //.fallbackToDestructiveMigration()//这个方法也可以迁移数据库，但会将数据摧毁导致数据的丢失
                    .addMigrations(MIGRATION_1_2)
                    .build();
        }
        return mDb;
    }

    /**
     * 数据库版本管理
     */
    static final Migration MIGRATION_1_2 =new Migration(1,2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {

        }
    };

    //新增
    public static synchronized void insertRoomType(Context context, RoomTypeBean roomType) {
        getInstance(context).roomDao().insert(roomType);
    }

    //新增
    public static synchronized void insertRoomTypes(Context context, List<RoomTypeBean> roomTypes) {
        getInstance(context).roomDao().insertList(roomTypes);
    }
    //查询
    public static synchronized RoomTypeBean getRoomType(Context context, int type) {
        return getInstance(context).roomDao().queryRoomType(type);
    }
    //删除
    public static synchronized void deleteRoomType(Context context, RoomTypeBean roomType) {
        getInstance(context).roomDao().deleteRoomType(roomType.getRoomType());
    }
    //修改
    public static synchronized void updateRoomType(Context context, RoomTypeBean roomType) {
        getInstance(context).roomDao().updateRoomType(roomType);
    }
    //获取用户信息
    public static synchronized List<RoomTypeBean> getRoomTypes(Context context) {
        return getInstance(context).roomDao().getAll();
    }

}
